//
//  ZZButton.m
//  ZZUIButton
//
//  Created by athena on 2018/1/24.
//  Copyright © 2018年 athena. All rights reserved.
//

#import "ZZButton.h"

@implementation ZZButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)init{
    self = [super init];
    if (self) {
        [self createView];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createView];
    }
    return self;
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        
        [self createView];
    }
    return self;
}
-(UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _imageSize.width, _imageSize.height)];
        [_imageView setBackgroundColor:[UIColor clearColor]];
        _imageView.userInteractionEnabled = NO;
        [self.contentView addSubview:_imageView];
    }
    return _imageView;
}
-(ZZLabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[ZZLabel alloc] init];
        _titleLabel.userInteractionEnabled = NO;
//        _titleLabel.textAlignment = NSTextAlignmentCenter;
//        _titleLabel.numberOfLines = 0;
        [_titleLabel setBackgroundColor:[UIColor clearColor]];
        __weak typeof (self) weakSelf = self;
        _titleLabel.viewChangedBlock = ^(ZZLabel *label){
            NSLog(@"changeFrame");
            [weakSelf updateUIFrame];
        };
        [self.contentView addSubview:_titleLabel];
    }
    return _titleLabel;
}
-(UILabel *)paopaoLabel{
    if (!_paopaoLabel) {
        _paopaoLabel = [[UILabel alloc] init];
        [_paopaoLabel setBackgroundColor:[UIColor redColor]];
        _paopaoLabel.userInteractionEnabled = NO;
    }
    return _paopaoLabel;
}
-(UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        [_contentView setBackgroundColor:[UIColor clearColor]];
        _contentView.userInteractionEnabled = NO;
    }
    return _contentView;
}

-(void)setSelected:(BOOL)selected{
    [super setSelected:selected];
    [self flushContentStyle];
}

-(void)setImageSize:(CGSize)imageSize{
    _imageSize = imageSize;
    CGRect imageViewFrame = self.imageView.frame;
    imageViewFrame.size = _imageSize;
    [self.imageView setFrame:imageViewFrame];
    [self updateUIFrame];
}
-(void)setImageOffset:(ZZButtonContentOffSet)imageOffset{
    _imageOffset = imageOffset;
    [self flushContentViewFrame];
    [self flushContentViewAlignment];
    [self flushContentStyle];
}
-(void)setImageLocation:(ZZButtonImageLocation)imageLocation{
    _imageLocation = imageLocation;
    [self updateUIFrame];
}
-(void)setContentAlignment:(ZZButtonContentAlignment)contentAlignment{
    _contentAlignment = contentAlignment;
    [self flushContentViewAlignment];
    [self flushContentStyle];
}
-(void)setContentOffSet:(ZZButtonContentOffSet)contentOffSet{
    _contentOffSet = contentOffSet;
    [self flushContentViewFrame];
    [self flushContentViewAlignment];
    [self flushContentStyle];
}
-(void)setContentMoreSize:(CGSize)contentMoreSize{
    _contentMoreSize = contentMoreSize;
    [self flushContentViewFrame];
    [self flushContentViewAlignment];
    [self flushContentStyle];
}

-(void)setDefaultImage:(UIImage *)defaultImage{
    _defaultImage = defaultImage;
    if (!self.isSelected) {
        [self.imageView setImage:_defaultImage];
    }
}
-(void)setSelectedImage:(UIImage *)selectedImage{
    _selectedImage = selectedImage;
    if (self.isSelected) {
        [self.imageView setImage:_selectedImage];
    }
}
-(void)setDefaultBackgroundColor:(UIColor *)defaultBackgroundColor{
    _defaultBackgroundColor = defaultBackgroundColor;
    if (!self.isSelected) {
        [self setBackgroundColor:_defaultBackgroundColor];
    }
}
-(void)setSelectedBackgroundColor:(UIColor *)selectedBackgroundColor{
    _selectedBackgroundColor = selectedBackgroundColor;
    if (self.isSelected) {
        [self setBackgroundColor:_selectedBackgroundColor];
    }
}
-(void)setDefaultTitleColor:(UIColor *)defaultTitleColor{
    _defaultTitleColor = defaultTitleColor;
    if (!self.isSelected) {
        [self.titleLabel setTextColor:_defaultTitleColor];
    }
}
-(void)setSelectedTitleColor:(UIColor *)selectedTitleColor{
    _selectedTitleColor = selectedTitleColor;
    if (self.isSelected) {
        [self.titleLabel setTextColor:_defaultTitleColor];
    }
}
-(void)setTitle:(NSString *)title{
    _title = title;
    if (!self.isSelected) {
        [self.titleLabel setText:_title];
        [self updateUIFrame];
    }
}
-(void)setSelectedTitle:(NSString *)selectedTitle{
    _selectedTitle = selectedTitle;
    if (self.isSelected) {
        [self.titleLabel setText:_selectedTitle];
        [self updateUIFrame];
    }
}

-(void)setAttributedString:(NSMutableAttributedString *)attributedString{
    _attributedString = attributedString;
    
    CGRect titleFrame=self.titleLabel.frame;
    switch (_imageLocation) {
        case  ZZButtonImageLocation_Top_ViewAlignmentLeft:
        case  ZZButtonImageLocation_Top_ViewAlignmentRight:
        case  ZZButtonImageLocation_Bottom_ViewAlignmentLeft:
        case ZZButtonImageLocation_Bottom_ViewAlignmentRight:
        case ZZButtonImageLocation_Top:
        case ZZButtonImageLocation_Bottom:
        {
            titleFrame.size.width = CGRectGetWidth(self.frame);
        }
            break;
        case ZZButtonImageLocation_Right:
        case ZZButtonImageLocation_Left:
        case ZZButtonImageLocation_BindTitleTopLeft:
        {
            titleFrame.size.width = CGRectGetWidth(self.frame) - _spacing - self.imageSize.width;
        }
            break;
        default:
            break;
    }
    [self.titleLabel setFrame:titleFrame];
    self.titleLabel.attributedText = _attributedString;
    [self.titleLabel sizeToFit];
    [self flushContentViewFrame];
    [self flushContentViewAlignment];
    [self flushContentStyle];
}
-(void)createView{
    _isFlushFrameWhenSelcted = YES;
    _imageSize = CGSizeZero;
    _spacing = 5.;
    _contentOffSet = ZZButtonContentOffSetMake(0, 0);
    _defaultBackgroundColor = [UIColor whiteColor];
    _defaultImage = nil;
    _defaultTitleColor = [UIColor blackColor];
    _title = @"";
   
    [self addSubview:self.contentView];
    [self updateUIFrame];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    UITouch* touch = [touches anyObject];
    [self beginTrackingWithTouch:touch withEvent:event];
    if (!self.isSelected) {
        [self flushContentStyleWithTouched:YES];
    }
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesCancelled:touches withEvent:event];
    [self cancelTrackingWithEvent:event];
    if (!self.isSelected) {
        [self flushContentStyleWithTouched:NO];
    }
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesMoved:touches withEvent:event];
    UITouch* touch = [touches anyObject];
    [self continueTrackingWithTouch:touch withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    UITouch* touch = [touches anyObject];
    [self endTrackingWithTouch:touch withEvent:event];
    if (!self.isSelected) {
        [self flushContentStyleWithTouched:NO];
    }
}
//-(void)layoutSubviews{
//    [super layoutSubviews];
//    NSLog(@"layoutSubviews");
//}
-(void)flushTitleLabelFrame{
    _attributedString = nil;
    CGSize titleLabelSize = CGSizeZero;
    float titleLabelMaxWidth = CGRectGetWidth(self.frame);
    float titleLabelMaxHeight = CGRectGetHeight(self.frame);
    if (!CGSizeEqualToSize(self.titleLabelMaxSizeWhenChanged, CGSizeZero)) {
        titleLabelMaxWidth = self.titleLabelMaxSizeWhenChanged.width;
        titleLabelMaxHeight = self.titleLabelMaxSizeWhenChanged.height;
    }
                                          
    if (self.titleLabel.text.length > 0) {
        switch (_imageLocation) {
            case ZZButtonImageLocation_Top:
            case ZZButtonImageLocation_Bottom:
            case ZZButtonImageLocation_Top_ViewAlignmentLeft:
            case ZZButtonImageLocation_Top_ViewAlignmentRight:
            case ZZButtonImageLocation_Bottom_ViewAlignmentLeft:
            case ZZButtonImageLocation_Bottom_ViewAlignmentRight:
            {
                titleLabelSize = [NSString getSizeToString:self.titleLabel.text
                                                   forFont:self.titleLabel.font
                                         constrainedToSize:CGSizeMake(titleLabelMaxWidth,(self.titleLabel.numberOfLines == 1) ? 25 : titleLabelMaxHeight - _imageSize.height - _spacing)
                                             lineBreakMode:self.titleLabel.lineBreakMode];
                
            }
                break;
            case ZZButtonImageLocation_Right:
            case ZZButtonImageLocation_Left:
            case ZZButtonImageLocation_BindTitleTopLeft:
            default:
            {
                titleLabelSize = [NSString getSizeToString:self.titleLabel.text
                                                   forFont:self.titleLabel.font
                                         constrainedToSize:CGSizeMake(titleLabelMaxWidth - _imageSize.width - _spacing, (self.titleLabel.numberOfLines == 1) ? 25 : titleLabelMaxHeight)
                                             lineBreakMode:self.titleLabel.lineBreakMode];
                
            }
                break;
        }
    }
    CGRect titleLabelFrame = self.titleLabel.frame;
    titleLabelFrame.size = titleLabelSize;
    [self.titleLabel setFrame:titleLabelFrame];
}
-(void)flushContentViewFrame{
    CGRect contentFrame = CGRectZero;
    float space = ((self.titleLabel.text.length > 0) && !CGSizeEqualToSize(self.imageView.frame.size, CGSizeZero)) ? _spacing : 0;
    switch (_imageLocation) {
        case  ZZButtonImageLocation_Top_ViewAlignmentLeft:{
            CGRect imageViewFrame = self.imageView.frame;
            CGRect titleLabelFrame = self.titleLabel.frame;
            
            contentFrame.size.width = (imageViewFrame.size.width > titleLabelFrame.size.width) ? imageViewFrame.size.width : titleLabelFrame.size.width;
            contentFrame.size.height = imageViewFrame.size.height + titleLabelFrame.size.height + space;
            
            imageViewFrame.origin.x = 0;
            imageViewFrame.origin.y = 0;
            if (self.imageOffset.x != 0 || self.imageOffset.y != 0) {
                imageViewFrame.origin.x += self.imageOffset.x;
                imageViewFrame.origin.y += self.imageOffset.y;
            }
            [self.imageView setFrame:imageViewFrame];
            
            titleLabelFrame.origin.x = 0;
            titleLabelFrame.origin.y = CGRectGetMaxY(imageViewFrame) + space;
            [self.titleLabel setFrame:titleLabelFrame];
        }
            break;
        case  ZZButtonImageLocation_Top_ViewAlignmentRight:{
            CGRect imageViewFrame = self.imageView.frame;
            CGRect titleLabelFrame = self.titleLabel.frame;
            
            contentFrame.size.width = (imageViewFrame.size.width > titleLabelFrame.size.width) ? imageViewFrame.size.width : titleLabelFrame.size.width;
            contentFrame.size.height = imageViewFrame.size.height + titleLabelFrame.size.height + space;
            
            imageViewFrame.origin.x = contentFrame.size.width - imageViewFrame.size.width;
            imageViewFrame.origin.y = 0;
            if (self.imageOffset.x != 0 || self.imageOffset.y != 0) {
                imageViewFrame.origin.x += self.imageOffset.x;
                imageViewFrame.origin.y += self.imageOffset.y;
            }
            [self.imageView setFrame:imageViewFrame];
            
            titleLabelFrame.origin.x = contentFrame.size.width - titleLabelFrame.size.width;
            titleLabelFrame.origin.y = CGRectGetMaxY(imageViewFrame) + space;
            [self.titleLabel setFrame:titleLabelFrame];
        }
            break;
        case  ZZButtonImageLocation_Bottom_ViewAlignmentLeft:{
            CGRect imageViewFrame = self.imageView.frame;
            CGRect titleLabelFrame = self.titleLabel.frame;
            
            contentFrame.size.width = (imageViewFrame.size.width > titleLabelFrame.size.width) ? imageViewFrame.size.width : titleLabelFrame.size.width;
            contentFrame.size.height = imageViewFrame.size.height + titleLabelFrame.size.height + space;
            
            titleLabelFrame.origin.x = 0;
            titleLabelFrame.origin.y = 0;
            [self.titleLabel setFrame:titleLabelFrame];
            
            imageViewFrame.origin.x = 0;
            imageViewFrame.origin.y = CGRectGetMaxY(titleLabelFrame) + space;
            if (self.imageOffset.x != 0 || self.imageOffset.y != 0) {
                imageViewFrame.origin.x += self.imageOffset.x;
                imageViewFrame.origin.y += self.imageOffset.y;
            }
            [self.imageView setFrame:imageViewFrame];
        }
            break;
        case ZZButtonImageLocation_Bottom_ViewAlignmentRight:{
            CGRect imageViewFrame = self.imageView.frame;
            CGRect titleLabelFrame = self.titleLabel.frame;
            
            contentFrame.size.width = (imageViewFrame.size.width > titleLabelFrame.size.width) ? imageViewFrame.size.width : titleLabelFrame.size.width;
            contentFrame.size.height = imageViewFrame.size.height + titleLabelFrame.size.height + space;
            
            titleLabelFrame.origin.x = contentFrame.size.width - titleLabelFrame.size.width;
            titleLabelFrame.origin.y = 0;
            [self.titleLabel setFrame:titleLabelFrame];
            
            imageViewFrame.origin.x = contentFrame.size.width - imageViewFrame.size.width;
            imageViewFrame.origin.y = CGRectGetMaxY(titleLabelFrame) + space;
            if (self.imageOffset.x != 0 || self.imageOffset.y != 0) {
                imageViewFrame.origin.x += self.imageOffset.x;
                imageViewFrame.origin.y += self.imageOffset.y;
            }
            [self.imageView setFrame:imageViewFrame];
        }
            break;
        case ZZButtonImageLocation_Top:
        {
            CGRect imageViewFrame = self.imageView.frame;
            CGRect titleLabelFrame = self.titleLabel.frame;
            
            contentFrame.size.width = (imageViewFrame.size.width > titleLabelFrame.size.width) ? imageViewFrame.size.width : titleLabelFrame.size.width;
            contentFrame.size.height = imageViewFrame.size.height + titleLabelFrame.size.height + space;
            
            imageViewFrame.origin.x = (contentFrame.size.width - imageViewFrame.size.width) / 2.;
            imageViewFrame.origin.y = 0;
            if (self.imageOffset.x != 0 || self.imageOffset.y != 0) {
                imageViewFrame.origin.x += self.imageOffset.x;
                imageViewFrame.origin.y += self.imageOffset.y;
            }
            [self.imageView setFrame:imageViewFrame];
            
            titleLabelFrame.origin.x = (contentFrame.size.width - titleLabelFrame.size.width) / 2.;
            titleLabelFrame.origin.y = CGRectGetMaxY(imageViewFrame) + space;
            [self.titleLabel setFrame:titleLabelFrame];
        }
            break;
            
        case ZZButtonImageLocation_Bottom:
        {
            CGRect imageViewFrame = self.imageView.frame;
            CGRect titleLabelFrame = self.titleLabel.frame;
            
            contentFrame.size.width = (imageViewFrame.size.width > titleLabelFrame.size.width) ? imageViewFrame.size.width : titleLabelFrame.size.width;
            contentFrame.size.height = imageViewFrame.size.height + titleLabelFrame.size.height + space;
            
            titleLabelFrame.origin.x = (contentFrame.size.width - titleLabelFrame.size.width) / 2.;
            titleLabelFrame.origin.y = 0;
            [self.titleLabel setFrame:titleLabelFrame];
            
            imageViewFrame.origin.x = (contentFrame.size.width - imageViewFrame.size.width) / 2.;
            imageViewFrame.origin.y = CGRectGetMaxY(titleLabelFrame) + space;
            if (self.imageOffset.x != 0 || self.imageOffset.y != 0) {
                imageViewFrame.origin.x += self.imageOffset.x;
                imageViewFrame.origin.y += self.imageOffset.y;
            }
            [self.imageView setFrame:imageViewFrame];
        }
            break;
        case ZZButtonImageLocation_Right:
        {
            CGRect imageViewFrame = self.imageView.frame;
            CGRect titleLabelFrame = self.titleLabel.frame;
            
            contentFrame.size.width = imageViewFrame.size.width + titleLabelFrame.size.width + space;
            contentFrame.size.height = (imageViewFrame.size.height > titleLabelFrame.size.height) ? imageViewFrame.size.height : titleLabelFrame.size.height;
            
            titleLabelFrame.origin.x = 0;
            titleLabelFrame.origin.y = (contentFrame.size.height - titleLabelFrame.size.height) / 2.;
            [self.titleLabel setFrame:titleLabelFrame];
            
            imageViewFrame.origin.x = CGRectGetMaxX(titleLabelFrame) + space;
            imageViewFrame.origin.y = (contentFrame.size.height - imageViewFrame.size.height) / 2.;
            if (self.imageOffset.x != 0 || self.imageOffset.y != 0) {
                imageViewFrame.origin.x += self.imageOffset.x;
                imageViewFrame.origin.y += self.imageOffset.y;
            }
            [self.imageView setFrame:imageViewFrame];
        }
            break;
        case ZZButtonImageLocation_Left:
        {
            CGRect imageViewFrame = self.imageView.frame;
            CGRect titleLabelFrame = self.titleLabel.frame;
            
            contentFrame.size.width = imageViewFrame.size.width + titleLabelFrame.size.width + space;
            contentFrame.size.height = (imageViewFrame.size.height > titleLabelFrame.size.height) ? imageViewFrame.size.height : titleLabelFrame.size.height;
            
            imageViewFrame.origin.x = 0;
            imageViewFrame.origin.y = (contentFrame.size.height - imageViewFrame.size.height) / 2.;
            if (self.imageOffset.x != 0 || self.imageOffset.y != 0) {
                imageViewFrame.origin.x += self.imageOffset.x;
                imageViewFrame.origin.y += self.imageOffset.y;
            }
            [self.imageView setFrame:imageViewFrame];
            
            titleLabelFrame.origin.x = CGRectGetMaxX(imageViewFrame) + space;
            titleLabelFrame.origin.y = (contentFrame.size.height - titleLabelFrame.size.height) / 2.;
            [self.titleLabel setFrame:titleLabelFrame];
        }
            break;
        case ZZButtonImageLocation_BindTitleTopLeft:
        {
            CGRect imageViewFrame = self.imageView.frame;
            CGRect titleLabelFrame = self.titleLabel.frame;
            
            contentFrame.size.width = imageViewFrame.size.width + titleLabelFrame.size.width + space;
            contentFrame.size.height = (imageViewFrame.size.height > titleLabelFrame.size.height) ? imageViewFrame.size.height : titleLabelFrame.size.height;
            
            imageViewFrame.origin.x = 0;
//            imageViewFrame.origin.y = (contentFrame.size.height - imageViewFrame.size.height) / 2.;
            imageViewFrame.origin.y = 0;
            if (self.imageOffset.x != 0 || self.imageOffset.y != 0) {
                imageViewFrame.origin.x += self.imageOffset.x;
                imageViewFrame.origin.y += self.imageOffset.y;
            }
            [self.imageView setFrame:imageViewFrame];
            
            titleLabelFrame.origin.x = CGRectGetMaxX(imageViewFrame) + space;
            if (CGRectGetHeight(imageViewFrame) > CGRectGetHeight(titleLabelFrame)) {
                titleLabelFrame.origin.y = (contentFrame.size.height - titleLabelFrame.size.height) / 2.;
            }else{
                titleLabelFrame.origin.y = 0;
            }
            [self.titleLabel setFrame:titleLabelFrame];
        }
            break;
        default:
            break;
    }
    if (!CGSizeEqualToSize(self.contentMoreSize, CGSizeZero)) {
        contentFrame.size.width += self.contentMoreSize.width;
        contentFrame.size.height += self.contentMoreSize.height;
        
        CGPoint imageViewCenter = self.imageView.center;
        CGPoint titleLabelCenter = self.titleLabel.center;
        imageViewCenter.y = contentFrame.size.height / 2.;
        titleLabelCenter.y = contentFrame.size.height / 2.;
        self.imageView.center = imageViewCenter;
        self.titleLabel.center = titleLabelCenter;
//        self.imageView.centerY = self.contentView.height / 2.;
//        self.titleLabel.centerY = self.contentView.height / 2.;
    }
    [self.contentView setFrame:contentFrame];
}
-(void)flushContentViewAlignment{
    CGRect contentViewFram = self.contentView.frame;
    switch (_contentAlignment) {
        case  ZZButtonContentAlignment_TopLeft:{
            
            contentViewFram.origin.y = 0;
            contentViewFram.origin.x = 0;
        }
            break;
        case ZZButtonContentAlignment_TopRight:{
            
            contentViewFram.origin.y = 0;
            contentViewFram.origin.x = CGRectGetHeight(self.frame) - contentViewFram.size.width;
        }
            break;
        case ZZButtonContentAlignment_BottomLeft:{
            contentViewFram.origin.y = CGRectGetHeight(self.frame) - contentViewFram.size.height;
            contentViewFram.origin.x = 0;
        }
            break;
        case ZZButtonContentAlignment_BottomRight:{
            
            contentViewFram.origin.y = CGRectGetHeight(self.frame) - contentViewFram.size.height;
            contentViewFram.origin.x = CGRectGetWidth(self.frame) - contentViewFram.size.width;
        }
            break;
        case ZZButtonContentAlignment_Top:
        {
            contentViewFram.origin.y = 0;
            contentViewFram.origin.x = (CGRectGetWidth(self.frame) - contentViewFram.size.width)/2.;
        }
            break;
        case ZZButtonContentAlignment_Left:
        {
            contentViewFram.origin.y = (CGRectGetHeight(self.frame) - contentViewFram.size.height)/2.;
            contentViewFram.origin.x = 0;
        }
            break;
        case ZZButtonContentAlignment_Bottom:
        {
            contentViewFram.origin.y = CGRectGetHeight(self.frame) - contentViewFram.size.height;
            contentViewFram.origin.x = (CGRectGetWidth(self.frame) - contentViewFram.size.width)/2.;
            
        }
            break;
        case ZZButtonContentAlignment_Right:
        {
            contentViewFram.origin.y = (CGRectGetHeight(self.frame) - contentViewFram.size.height)/2.;
            contentViewFram.origin.x = CGRectGetWidth(self.frame) - contentViewFram.size.width;
        }
            break;
        case ZZButtonContentAlignment_Center:
        default:
        {
            contentViewFram.origin.y = (CGRectGetHeight(self.frame) - contentViewFram.size.height)/2.;
            contentViewFram.origin.x = (CGRectGetWidth(self.frame) - contentViewFram.size.width)/2.;
        }
            break;
    }
    contentViewFram.origin.x += _contentOffSet.x;
    contentViewFram.origin.y += _contentOffSet.y;
    [self.contentView setFrame:contentViewFram];
}
-(void)flushContentStyle{
    [self flushContentStyleWithTouched:self.isSelected];
}
-(void)flushContentStyleWithTouched:(BOOL)isTouched{
    if (isTouched) {
        if (_selectedBackgroundColor) {
            [self setBackgroundColor:_selectedBackgroundColor];
        }
        if (_selectedImage) {
            [self.imageView setImage:_selectedImage];
        }
        if (_selectedTitleColor && !_attributedString) {
            [self.titleLabel setTextColor:_selectedTitleColor];
        }
        if (_selectedTitle && !_attributedString) {
            [self.titleLabel setText:_selectedTitle];
        }
    }else{
        if (_defaultBackgroundColor) {
            [self setBackgroundColor:_defaultBackgroundColor];
        }
        if (_defaultImage) {
            [self.imageView setImage:_defaultImage];
        }
        if (_defaultTitleColor && !_attributedString) {
            [self.titleLabel setTextColor:_defaultTitleColor];
        }
        if (_selectedTitle && !_attributedString) {
            [self.titleLabel setText:_selectedTitle];
        }
        if (_title && !_attributedString) {
            [self.titleLabel setText:_title];
        }
    }
    if (_isFlushFrameWhenSelcted) {
        [self flushTitleLabelFrame];
        [self flushContentViewFrame];
        [self flushContentViewAlignment];
    }
}
-(void)updateUIFrame{
    [self flushTitleLabelFrame];
    [self flushContentViewFrame];
    [self flushContentViewAlignment];
    [self flushContentStyle];
}
-(void)layoutSubviews{
    [super layoutSubviews];
    [self flushContentViewAlignment];
    if (self.layoutSubviewsBlock) {
        self.layoutSubviewsBlock();
    }
}


@end
